package com.dg11185.nearshop.net.weixin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.dg11185.nearshop.net.response.NetStatus;

/**
 * 
 * @author：Daibo    
 * @time：2014-9-24 上午9:49:23
 */
public class TabloidResponse {

    public NetStatus status;

    public List<Tabloid> tabloids = null;

    public TabloidResponse() {
        tabloids = new ArrayList<Tabloid>();
    }

    public void parseJson(String response) {

        try {
            JSONObject object = new JSONObject(response);
            status = NetStatus.getStatus(object.optString("status"));
            if (!object.isNull("source_news")) {
                JSONArray array = object.optJSONArray("source_news");
                for (int a = 0; a < array.length(); a++) {
                    List<Tabloid> tabloids2 = new ArrayList<Tabloid>();
                    JSONObject object2 = array.getJSONObject(a);
                    String remark = object2.optString("remark");
                    String createTime = object2.optString("create_time");

                    JSONArray array2 = object2.optJSONArray("articles");
                    for (int m = 0; m < array2.length(); m++) {
                        Tabloid tabloid = new Tabloid();
                        JSONObject object3 = array2.getJSONObject(m);
                        tabloid.remark = remark;
                        tabloid.createTime = createTime;
                        tabloid.title = object3.optString("title");
                        tabloid.description = object3.optString("description");
                        tabloid.picUrl = object3.optString("picUrl");
                        tabloid.webUrl = object3.optString("webUrl");
                        tabloids2.add(tabloid);
                    }

                    Collections.reverse(tabloids2);
                    tabloids.addAll(tabloids2);
                }
                Collections.reverse(tabloids);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    public static List<Tabloid> getTabloids() {
        List<Tabloid> tabloids = new ArrayList<Tabloid>();
        for (int a = 0; a < 20; a++) {
            Tabloid tabloid = new Tabloid();
            tabloid.remark = "000";
            tabloid.createTime = "2014-08-22";
            tabloid.title = "title";
            tabloid.description = a + "   三星以2亿美元收购物联网公司SmartThings";
            tabloid.picUrl = "http://a.36krcnd.com/photo/2014/e9db4aad19e442736842f87f620eaa96.jpg";
            tabloid.webUrl = "http://www.36kr.com/p/214574.html";
            tabloids.add(tabloid);
        }
        for (int a = 0; a < tabloids.size(); a++) {
            if (a < 4) {
                tabloids.get(a).remark = "4";
            } else if (a < 6) {
                tabloids.get(a).remark = "6";
            } else if (a < 7) {
                tabloids.get(a).remark = "7";
            } else if (a < 8) {
                tabloids.get(a).remark = "8";
            } else if (a < 11) {
                tabloids.get(a).remark = "11";
            } else if (a < 15) {
                tabloids.get(a).remark = "15";
            } else if (a < 20) {
                tabloids.get(a).remark = "21";
            }
        }
        return tabloids;
    }
}
